package com.project.controller.goodsOrderController;

import com.github.pagehelper.PageInfo;
import com.project.bean.GoodsBean;
import com.project.bean.GoodsOrderBean;
import com.project.bean.GoodsOrderInfoBean;
import com.project.bean.ShopBean;
import com.project.service.goodsOrderInfoService.IGoodsOrderInfoService;
import com.project.service.goodsOrderService.IGoodsOrderService;
import com.project.service.goodsService.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/goodsOrder")
public class GoodsOrderController {
    @Autowired
    private IGoodsOrderService orderService;
    @Autowired
    private IGoodsOrderInfoService goodsOrderInfoService;
    @Autowired
    private IGoodsService goodsService;

    @RequestMapping("/findAll")
    public PageInfo<GoodsOrderBean> findAll(int pageNO, int storeId){
        return orderService.findAll(pageNO, storeId);
    }
    @RequestMapping("/submit")
    public String add(GoodsOrderBean goodsOrder,int userId, HttpServletRequest request){
        Map<Integer, ShopBean> shopMap = (Map<Integer, ShopBean>) request.getSession().getAttribute("ShopMap");
        if("".equals(shopMap.values()) || shopMap.size() == 0) {
            return "no";
        }
        orderService.add(goodsOrder,shopMap,userId);
        shopMap.clear();
        return "ok";
    }
    @RequestMapping("/findById")
    public GoodsOrderBean findById(int orderId){
        return orderService.findById(orderId);
    }

    @RequestMapping("/goodsOrderStart")
    public String updateStart(Integer orderId, int[] orderInfoId){
        orderService.update(orderId, "发货中");
        for(int n :orderInfoId){
            goodsOrderInfoService.update(n, "已发货");
        }
        return "ok";
    }

    @RequestMapping("/goodsOrderInfoEnd")
    public String updateEnd(int orderInfoId){
        goodsOrderInfoService.update(orderInfoId, "已完结");
        return "ok";
    }

}
